package org.example;



import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;


/**
 * @Author: duhang
 * @Date: 2022/8/25
 * @Description: selenium框架，网页测试自动化
 **/
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        //读取excel
        String filePath = "G:\\Desktop\\data.xls";

        FileInputStream fileInputStream = new FileInputStream(filePath);

        XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
        XSSFSheet sheetAt = sheets.getSheetAt(0);
        ArrayList<String> companys = new ArrayList<>();
        //获取全部公司名称
        for (int i = 1190; i < 1970; i++) { // excel 行数 1190 -- 1970  todo设置读取的行范围
            XSSFRow row = sheetAt.getRow(i);
            XSSFCell cell = row.getCell(0);
            String s = cell.getStringCellValue();
            companys.add(s);
        }

        //打开浏览器
        String chromeDriver = "G:\\Desktop\\search\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", chromeDriver);

        ChromeDriver browser = new ChromeDriver();
        browser.get("https://webvpn.ujs.edu.cn/login");


        //点击登录按钮
        WebElement login = browser.findElement(By.xpath("//*[@id=\"cas-login\"]"));
        login.click();

        //点击短信登录
        WebElement message = browser.findElement(By.xpath("//*[@id=\"dyLogin\"]"));
        message.click();

        //输入手机号
        WebElement phone = browser.findElement(By.xpath("/html/body/div[4]/div[2]/div[2]/div/div[3]/div[2]/form/p[1]/input"));
        phone.sendKeys("15182214528");


        //等待验证码
        //todo debug模式运行到此输入验证码后，放行即可，自动完成检索
        //点击登录
        WebElement loginButton = browser.findElement(By.xpath("//*[@id=\"casDynamicLoginForm\"]/p[5]/button"));
        loginButton.click();

        //睡两秒等待网页渲染
        Thread.sleep(2000);
        //点击图书馆数据库
        browser.findElement(By.xpath("/html/body/div/div/div/div/div[3]/div/div[2]/div/div[1]/div/div[1]/div/div[1]/a/div[2]")).click();
        Thread.sleep(2000);

        //切换窗口
        Set<String> windowHandles = browser.getWindowHandles();
        String nextWindow = null;
        for (String windowHandle : windowHandles) {
            if(!windowHandle.equals(browser.getWindowHandle())){
                nextWindow = windowHandle;
            }
        }

        //切换窗口
        browser.switchTo().window(nextWindow);

        Thread.sleep(3000);
        int step = 500;
        //下滑页面
        while(true){
            try{
                //点击更多数据库
                browser.findElement(By.xpath("/html/body/div[6]/div[1]/div/div[1]/h4/span/a")).click();
                break;
            }catch (Exception e){
                browser.executeScript("window.scrollBy(0,"+ step +")");
                step += 200;
            }
        }


        Thread.sleep(2000);
        //数据库搜索输入框
        WebElement databaseSearch = browser.findElement(By.xpath("/html/body/div[5]/form/table[1]/tbody/tr[1]/td[2]/input[1]"));
        //输入壹专利
        databaseSearch.sendKeys("壹专利");
        //点击查询
        browser.findElement(By.xpath("/html/body/div[5]/form/table[1]/tbody/tr[1]/td[2]/input[2]")).click();
        Thread.sleep(2000);

        //点击入口
        browser.findElement(By.xpath("/html/body/div[5]/form/table[2]/tbody/tr[2]/td[6]/a")).click();

        Thread.sleep(3000);
        //点击登录按钮
        browser.findElement(By.xpath("/html/body/div[1]/div/div[1]/div[3]/a[2]/a")).click();
        Thread.sleep(800);
        //点击iP登录
        browser.findElement(By.xpath("/html/body/div[1]/div/div[2]/div[2]/div[1]/div[1]/div[1]/ul/li[4]/a")).click();
        Thread.sleep(800);
        //点击立即加入
        browser.findElement(By.xpath("/html/body/div[1]/div/div[2]/div[2]/div[1]/div[1]/div[2]/ul/li[4]/div/div/a")).click();
        //进入数据库页面之后
        Thread.sleep(2000);
        //删除弹框
        browser.findElement(By.xpath("/html/body/div[5]/a")).click();
        //获取搜索框，随机搜索一个
        browser.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[2]/div/div[1]/div/div[2]/div[2]/div[1]/div")).sendKeys("随机搜索");
        //点击搜索
        browser.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[2]/div/div[1]/div/div[2]/a")).click();

        Thread.sleep(2000);

        //点击筛选字段
        browser.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[1]/div/div/div[2]/div/div[1]/a[1]")).click();

        Thread.sleep(3000);
        //点击选中诉讼次数
        browser.findElement(By.xpath("/html/body/div[4]/div[2]/div/div[1]/div[1]/div/div[6]/div/div/div[8]/label/span[1]")).click();

        //点击确定
        browser.findElement(By.xpath("/html/body/div[4]/div[3]/a[1]")).click();



        Thread.sleep(1000*3);
        WebElement input = browser.findElement(By.xpath("/html/body/div[1]/div/div[1]/div[2]/div[3]/div[1]/div"));
        WebElement search = browser.findElement(By.xpath("/html/body/div[1]/div/div[1]/div[2]/a"));


        ArrayList<Integer> validateNum = new ArrayList<>();
        ArrayList<Integer> num = new ArrayList<>();

        for (String company : companys) {
            //清空输入框
            input.sendKeys(Keys.CONTROL,"a");
            input.sendKeys(Keys.DELETE);
            //填充搜索关键字
            input.sendKeys(company);
            //点击搜索
            search.click();
            //睡三秒等待，网页渲染
            Thread.sleep(6000);

            browser.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[1]/div/div/div[2]/div/div[2]/ul/li[2]/a/i")).click();
            Thread.sleep(4000);
            for (int i = 1 ; i <= 3 ; i++ ) {
                String path = "/html/body/div[1]/div/div[2]/div/div[1]/div/div/div[2]/div/div[2]/ul/li[2]/ul/li["+i+"]";
                WebElement element = null;
                try{
                    element = browser.findElement(By.xpath(path));
                }catch (Exception e){
                    validateNum.add(0);
                   break;
                }
                if(element != null){
                    String text = null;
                    try{
                        text = browser.findElement(By.xpath(path + "/div[1]/span/span")).getText();
                    }catch (Exception e){
                        validateNum.add(0);
                        break;
                    }
                    if(text.equals("有效")){
                        String valid = null;
                        try{
                            //专利有效次数
                            valid = browser.findElement(By.xpath(path + "/div[2]")).getText();
                        }catch (Exception e){
                            validateNum.add(0);
                            break;
                        }
                        validateNum.add(Integer.parseInt(valid));
                        break;
                    }
                }
            }

            browser.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[1]/div/div/div[2]/div/div[2]/ul/li[14]/a/i")).click();
            Thread.sleep(4000);
            int index = 1;
            int sum = 0;
            while(true){
                String path = "/html/body/div[1]/div/div[2]/div/div[1]/div/div/div[2]/div/div[2]/ul/li[14]/ul/li["+index+"]";
                WebElement element = null;
                try{
                    element = browser.findElement(By.xpath(path));
                }catch (Exception e){
                    break;
                }
                String nn = null;
                try{
                    //诉讼次数
                    nn = browser.findElement(By.xpath(path + "/div[1]/span/span")).getText();

                }catch (Exception e){
                    break;
                }
                int i = Integer.parseInt(nn);
                sum += i;
                index++;
            }
            num.add(sum);
        }

        //得到数据后，选择要写入的文件，进行写入，也可以写入原来的文件，这里选择新建文件写入
        //读取excel
        String filePathres = "G:\\Desktop\\ans.xlsx"; // 写入 文件的目录

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheetAt1 = wb.createSheet("res");
        FileOutputStream fileOutputStream = new FileOutputStream(filePathres);

        for (int i = 0; i < validateNum.size(); i++) {

            //创建一行
            XSSFRow row = sheetAt1.createRow(i);

            //公司名称
            XSSFCell cell1 = row.createCell(0);
            cell1.setCellValue(companys.get(i));

            //诉讼次数
            XSSFCell cell = row.createCell(1);
            cell.setCellValue(num.get(i));

            //有效次数
            XSSFCell cell2 = row.createCell(2);
            cell2.setCellValue(validateNum.get(i));
        }
        fileOutputStream.flush();
        wb.write(fileOutputStream);
        wb.close();
        fileOutputStream.close();
    }
}
